
package server;

import SBLib.Message;
import SBLib.User;
import SBLib.File;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 
 * @author BrandonNeal
 */
public class ServerWorker extends Thread {

    //Following 4 variables are passed in by Server
    private Socket clientSocket;
    final private ArrayList<File> Files;
    final private ArrayList<User> Users;
    //public Replicator replicator;
        
    //Streams to communicate with client
    private ObjectInputStream in;
    private ObjectOutputStream out;
    
    

    public ServerWorker(Socket cSocket, ArrayList<File> files, ArrayList<User> users) {
        this.Files = files;
        this.Users = users;
        //this.replicator = replicator;
     
          
        this.clientSocket = cSocket;
        this.start(); //Start Worker Thread
        
       
    }

    /**
     * - Creates a request Handler, and manages communication between requestHandler and the client.
     * - It is a constant connection with the user, so this method is just always sending message from client to requestHandler, 
     *  then sending response message back to client.
     */
    @Override
    public void run() {
        RequestHandler requestHandler = new RequestHandler(this.Users, this.Files);
            try {
                
                this.out = new ObjectOutputStream(this.clientSocket.getOutputStream());
                this.out.flush();
                this.in = new ObjectInputStream(this.clientSocket.getInputStream());
                while(true)
                {
                    System.out.println("Srver worker is waiting to read in");
                    String fromClient = (String) this.in.readObject();
                    System.out.println("Recieved:" + fromClient);
                    this.SendToClient(requestHandler.HandleRequest(fromClient));
                }
            } catch (IOException ex) {
                Logger.getLogger(ServerWorker.class.getName()).log(Level.SEVERE, null, ex);
                
                //requestHandler.RemoveUserLocks();
                
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ServerWorker.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
   
    /**
     * *
     * Writes to out stream (back to client)
     *
     * @param message Message to send to client
     */
    private void SendToClient(String message) {
        try {
            if(Server.STUPID_MODE)
            {
                Random generator = new Random();
                Integer randomIndex = generator.nextInt();
                String stupidResponse = randomIndex.toString();
                System.out.println("IN STUPID MODE:" + stupidResponse);
                this.out.writeObject(stupidResponse);
            }
            else
                this.out.writeObject(message);
        } catch (IOException ex) {
            Logger.getLogger(ServerWorker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
